<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public Map getMap(List<Map> result , Map<String,String> deptMap , Map<String,String> ssMap){
	
		//处理结合数据
		for(Map map : result){
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("deptName",deptMap.get(ksdm));
			String ssdm = map.get("ssdm1") == null ? "0" : map.get("ssdm1").toString();
			map.put("ssmc",ssMap.get(ssdm));
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
	
		return resMap;
	}
%>
<%
	//统计危重病人
	String yydm =  input.getDesc("yydm","01");//医院代码
	String date = input.getDesc("start_date","2018-03-06");//开始时间
	String endDa = input.getDesc("end_date","2018-03-06");//结束时间
	String ksdm =  input.getDesc("ksdm","-1");//科室代码
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	
	String sqlWhere = "";
	if(!ksdm.equals("-1")){
		sqlWhere += " and a.ksdm in("+ksdm+")";
		
	}
	if(!yydm.equals("-1"))
	sqlWhere += " and a.ksdm in (select id from YY_KSBMK where yydm ='"+yydm+"') " ;
	
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept where yydm='"+yydm+"'");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	//获取所有的手术集合
	List<Map> ssList = QBuilder.db("basedb").queryHql("select new map(xh as xh , ssmc as ssmc ) from SS_SSDJK ");
	Map<String,String> ssMap = new HashMap<String,String>();
	//手术放到map中 key-value(id-name)
	for(Map map : ssList){
		ssMap.put(map.get("xh").toString(),map.get("ssmc").toString());
	}
	
	//查询危重病人
	//String hql = "select new map( a.syxh as syxh , a.blh as blh , a.cwdm as cwdm ,a.hzxm as hzxm , a.sex  as sex , a.ksdm as ksdm , a.bqdm as bqdm , a.zddm as zddm , a.ryrq as ryrq ,a.cyrq as cyrq ,  a.ssdm1 as ssdm1 ,a.ssdm2 as ssdm2 ,a.wzjb as wzjb  , a.memo as memo) from ZY_BRSYK a , BQ_CQYZK b where a.syxh = b.syxh and ( b.yzlb = 16 or b.yzlb = 17 ) and b.ksrq >= '" + startDate + "' and b.ksrq <= '" + endDate + "' and b.yzzt in(0,1,2) order by ksrq desc";//
	String sql = "select  a.syxh as syxh , a.blh as blh , a.cwdm as cwdm ,a.hzxm as hzxm , a.sex  as sex , a.ksdm as ksdm , a.bqdm as bqdm , a.zddm as zddm , a.ryrq as ryrq ,a.cyrq as cyrq ,  a.ssdm1 as ssdm1 ,a.ssdm2 as ssdm2 ,a.wzjb as wzjb  , a.memo as memo from ZY_BRSYK a left join ( select DISTINCT syxh ,bqdm, yzlb  ,yzzt from  BQ_CQYZK ) b on a.syxh = b.syxh where  ( b.yzlb = 16 or b.yzlb = 17 ) and ((cyrq is null or  cqrq > '"+endDate+"')  and (rqrq < '"+endDate+"' ) )  and  brzt != 9   and b.yzzt in(0,1,2) "+sqlWhere+" order by a.ryrq desc";

	List<Map> result = new ArrayList<Map>();
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
      
	try {
		 ss.beginTransaction();
		 SQLQuery sqlQuery = ss.createSQLQuery(sql);
		 sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        result = sqlQuery.list();
		  ss.getTransaction().commit();
		 ss.close();
	
    }catch (Exception e){
	     ss.getTransaction().commit();
		 ss.close();
    }
	//List<Map> result = QBuilder.db("basedb").queryHql(hql);

	for(Map map : result){
			SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddhh:mm:ss");
			SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
			if(map.get("ryrq") != null ){
					map.put("ryrq",sdf4.format(sdf3.parse(map.get("ryrq").toString())));
			}
			if(map.get("cyrq") != null ){
					map.put("cyrq",sdf4.format(sdf3.parse(map.get("cyrq").toString())));
			}
		
	}
	
	Map<String,Object> resMap = new HashMap<String,Object>();
	resMap.put("ksMap",getMap(result , deptMap , ssMap));
	
	
	
	Message msg = new Message();

	msg.put("critical",resMap);
	out.println(msg);
%>